<template>
  <div class="LoginPanel">
    <h1 class="title">后台管理系统</h1>
    <el-tabs type="border-card" stretch v-model="currentTab">
      <el-tab-pane name="account">
        <template #label>
          <div>
            <user-filled class="icon-style" />
            <span>账号登录</span>
          </div>
        </template>
        <LoginAccount ref="accountRef" />
      </el-tab-pane>
      <el-tab-pane name="phone">
        <template #label>
          <div>
            <iphone class="icon-style" />
            <span>手机登录</span>
          </div>
        </template>
        <LoginPhone />
      </el-tab-pane>
    </el-tabs>
    <div class="password-operation">
      <el-checkbox v-model="isKeepPassword">记住密码</el-checkbox>
      <el-link type="primary">忘记密码</el-link>
    </div>
    <el-button type="primary" class="login-btn" @click="handleLoginClick">
      立即登录
    </el-button>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import LoginAccount from './LoginAccount.vue'
import LoginPhone from './LoginPhone.vue'

export default defineComponent({
  components: {
    LoginAccount,
    LoginPhone
  },
  setup() {
    //控制选项卡切换
    const currentTab = ref('account')
    //是否记住密码
    const isKeepPassword = ref(false)
    const accountRef = ref<InstanceType<typeof LoginAccount>>()
    const handleLoginClick = () => {
      if (currentTab.value === 'account') {
        accountRef.value?.loginAction(isKeepPassword.value)
      }
      /*  const url = '/api/login'
      const xhr = new XMLHttpRequest()
      xhr.onload = () => {
        if (xhr.readyState !== 4) return
        if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304) {
          console.log(xhr.response)
          console.log(typeof xhr.response)
        } else {
          console.log(xhr.response)
        }
      }
      xhr.open('post', url, true)
      xhr.setRequestHeader('Content-type', 'application/json')
      xhr.send(JSON.stringify(data)) */
    }
    return { currentTab, isKeepPassword, accountRef, handleLoginClick }
  }
})
</script>

<style scoped lang="less">
.LoginPanel {
  width: 320px;
  .title {
    text-align: center;
    margin: 0;
    margin-bottom: 20px;
  }
  .icon-style {
    width: 1em;
    height: 1em;
    margin-right: 5px;
    position: relative;
    top: 2px;
  }
  .password-operation {
    display: flex;
    justify-content: space-between;
    margin-top: 10px;
  }
  .login-btn {
    width: 100%;
    margin-top: 10px;
  }
}
</style>
